package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findCategoryByType(Integer type) {
        PageHelper.clearPage();
        List<Category> categoryByType = categoryMapper.findCategoryByType(type);
        return categoryByType;
    }
    @Override
    public PageResult pageByCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categories = categoryMapper.pageByCategory(categoryPageQueryDTO);

        PageResult pr = new PageResult(categories.getTotal(),categories.getResult());
        return pr;
    }

    @Override
    public int addCategory(CategoryDTO categoryDTO) {
        Category category  = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        int row = categoryMapper.addCategory(category);
        return row;
    }

    @Override
    public int deleteCategoryById(Integer id) {
        int row = categoryMapper.deleteCategoryById(id);
        return row;
    }

    @Override
    public int updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        int row = categoryMapper.updateCategory(category);
        return row;
    }

    @Override
    public int updateStatus(Integer status, Long id) {
        int row = categoryMapper.updateStatus(status, id);
        return row;
    }


}
